package storageManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class RecordOptions{
	TableRecord tableRecord;
	IndexFile indexFile;
	DataFile dataFile;
	public RecordOptions(TableRecord record) throws IOException{
		this.tableRecord = record;
		this.indexFile = new IndexFile(record);
		this.dataFile = new DataFile(record);
	}
	public void addRecord() throws IOException{//This method interacts with the user and gets the record values from him or her. It also gives warnings if the user enters unwanted values and ask for the value again.
		boolean ok = false;
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter the values the fields(First value you enter is going to be the value of the key field): ");
		ArrayList<String> values = new ArrayList<String>();
		String keyValue = "";
		while(!ok){
			System.out.println("Enter the value of the key field: ");
			keyValue = scan.nextLine();
			if(!checkKeyValue(keyValue, tableRecord.getFieldSizes().get(0), "key value")){
				ok=false;
			}else{
				ok=true;
			}
		}
		values.add(keyValue);
		
		String value = "";
		for(int i=1;i<tableRecord.getNumberOfFields();i++){
			value = "";
			ok = false;
			while(!ok){
				System.out.println("Enter the value of the "+(i+1)+". field: ");
				value = scan.nextLine();
				if(!Tools.checkTheName(tableRecord.getFieldSizes().get(i), value, "value")){
					ok=false;
				}else{
					ok=true;
				}
			}
			values.add(value);
		}
		DataFile dataFile = new DataFile(tableRecord);
		ArrayList<Integer> placeOfDataRecord = dataFile.addDataRecord(values);
		indexFile.addIndexrecord(new IndexRecord(keyValue, placeOfDataRecord,tableRecord));
	}

	public void deleteARecord() throws IOException{//Gets the keyValue from the user and deletes it. It gives warning if there is not a record with the given keyValue
		Scanner scan = new Scanner(System.in);
		boolean ok = false;
		String keyValue="";
		while(!ok){
			System.out.println("Enter the key value: ");
			keyValue = scan.nextLine();
			if(!Tools.checkTheName(tableRecord.getFieldSizes().get(0), keyValue, "key value")){
				
			}else if(!indexFile.isInTable(keyValue)){
				System.out.println("There is not a key value named "+keyValue+"!");
			}else{
				ok=true;
			}
		}
		ArrayList<Integer> placeOfDeletedRecordInDataFile = indexFile.deleteIndexRecord(keyValue);
		dataFile.deleteARecord(placeOfDeletedRecordInDataFile);
	}
	public void searcForARecord() throws IOException{//Finds and writes the values of the record that has the keyValue which is acquired from the user.
		DataRecord foundDataRecord = dataFile.findDataRecord(findIndexRecord());
		for(int i=0;i<tableRecord.getNumberOfFields();i++)
			System.out.print(tableRecord.getFieldNames().get(i)+"\t");
		System.out.println();
		for(int i=0;i<foundDataRecord.getValues().size();i++)
			System.out.print(foundDataRecord.getValues().get(i)+"\t");
	}
	public void listAllRecords() throws IOException{//Lists every records in the ascending order
		ArrayList<DataRecord> dataRecords = dataFile.listOfAllDataRecords(indexFile.listOfIndexrecords());
		listTheDataRecords(dataRecords);
	}
	public void listAllRecordsSmallerThan() throws IOException{//Lists every record of which keyValue is less than the given keyValue, in ascending order. 
		System.out.println("Please enter a value");
		Scanner scan = new Scanner(System.in);
		String value = scan.next();
		listTheDataRecords(dataFile.listOfAllDataRecords(indexFile.smallerThan(value)));
	}
	public boolean checkKeyValue(String keyValue,int upperLimt,String indicator) throws IOException{//Check if the given keyValue is ok
		if(!Tools.checkTheName(upperLimt, keyValue,indicator)){//Check if it has proper size.
			return false;
		}else if(indexFile.isInTable(keyValue)){//Checks if there is already a record with the given keyValue
			System.out.println("There is a key value named "+keyValue+"!");
			return false;
		}
		return true;	
	}
	public IndexRecord findIndexRecord() throws IOException{
		Scanner scan = new Scanner(System.in);
		boolean ok = false;
		String keyValue="";
		while(!ok){
			System.out.println("Enter the key value: ");
			keyValue = scan.nextLine();
			if(!Tools.checkTheName(tableRecord.getFieldSizes().get(0), keyValue, "key value")){
				
			}else if(!indexFile.isInTable(keyValue)){
				System.out.println("There is not a key value named "+keyValue+"!");
			}else{
				ok=true;
			}
		}
		return indexFile.findTheRecord(keyValue);
	}
	public void listTheDataRecords(ArrayList<DataRecord> dataRecords){
		for(int i=0;i<tableRecord.getNumberOfFields();i++)
			System.out.print(tableRecord.getFieldNames().get(i)+"\t");
		System.out.println();
		for(DataRecord dataRecord:dataRecords){
			for(int i=0;i<dataRecord.getValues().size();i++)
				System.out.print(dataRecord.getValues().get(i)+"\t");
			System.out.println();
		}
	}
}
